package com.lins.common.mp.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lins.common.mp.handler.FillMetaObjectHandler;
import com.lins.common.mp.handler.FillMetaService;
import org.springframework.beans.factory.annotation.Value;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @author linsir
 * @title: BaseMPConfig
 * @projectName lins
 * @description: mybatis 基本配置
 * @date 2021/12/9 23:49
 */

public class BaseMPConfig {

    public GlobalConfig.DbConfig dbConfig()
    {
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setTableUnderline(true); // 下划线
        dbConfig.setCapitalMode(true); //驼峰命名
        return dbConfig;
    }


    public GlobalConfig globalConfig(FillMetaService fillMetaService)
    {
          GlobalConfig globalConfig = new GlobalConfig().setDbConfig(dbConfig());
          globalConfig.setMetaObjectHandler(new FillMetaObjectHandler(fillMetaService));
          return globalConfig;

    }


    /**
     * 关于 mybatis 内部拦截器插件
     *
     * BlockAttackInnerInterceptor  防止全表删除的插件
     * DynamicTableNameInnerInterceptor 动态表插件
     * IllegalSQLInnerInterceptor：不规范SQL拦截器
     * OptimisticLockerInnerInterceptor 乐观锁
     * PaginationInnerInterceptor 分页
     * TenantLineInnerInterceptor 多租户
     *
     */

    /**
     *
     * 分页组件
     * */
    public PaginationInnerInterceptor paginationInnerInterceptorMYSQL()
    {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        return paginationInnerInterceptor;
    }

    /**
     * 默认封装  mysql的分页组件
     * @return
     */
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
        MybatisPlusInterceptor mybatisPlusInterceptor =new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptorMYSQL());
        return mybatisPlusInterceptor;
    }

}
